package com.atguigu.gmall.payment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.atguigu.gmall.model.enums.PaymentType;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.payment.config.AlipayConfig;
import com.atguigu.gmall.payment.mapper.OrderInfoMapper;
import com.atguigu.gmall.payment.service.AlipayService;
import com.atguigu.gmall.payment.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AlipayServiceImpl implements AlipayService {
    @Autowired
    private AlipayClient alipayClient;
    @Autowired
    private PaymentService paymentService;
    //统一收单 下单并支持接口
    @Override
    public String submit(Long orderId) {
        /**
         * 实例化客户端
         */
        //1:网关地址
        //2:AppID
        //3:私钥
        //4:发送的数据格式
        //5:数据的编码
        //6:支付宝公司的公钥
        //7:加密方式

        /**
         * 接口名称 8:统一收单下单 并支持接口
         */
        //在生成统一下单接口时,要先生成支付表
        PaymentInfo paymentInfo = paymentService.insert(orderId, PaymentType.ALIPAY);
        //实例化客户端 在配置类中,注入了spring容器
        //8:统一收单下单 并支持接口   alipay.trade.page.pay(统一收单下单并支付页面接口)
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        // 异步回调  notify
        request.setNotifyUrl(AlipayConfig.notify_payment_url);
        // 同步回调
        request.setReturnUrl(AlipayConfig.return_payment_url);

        //请求参数
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no",paymentInfo.getOutTradeNo()); //支付宝交易编号 唯一必选
        bizContent.put("total_amount",paymentInfo.getTotalAmount()); //总金额,必选
        bizContent.put("subject", paymentInfo.getSubject());//必选
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");//注：目前仅支持FAST_INSTANT_TRADE_PAY必选
        //Calendar calendar  = Calendar.getInstance(); 订单绝对超时时间
        //calendar.add(Calendar.HOUR,1);
        //bizContent.put("time_expire", calendar.getTime());//二维码的  绝对时间  16:30  可选
        System.out.println("json:" + bizContent.toJSONString());
        System.out.println("String:" + bizContent.toString());

        request.setBizContent(bizContent.toString());
        //发出此请求
        AlipayTradePagePayResponse alipayTradePagePayResponse = null;
        try {
            alipayTradePagePayResponse = alipayClient.pageExecute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        if (alipayTradePagePayResponse.isSuccess()){
            System.out.println("调用成功");
            return alipayTradePagePayResponse.getBody();
        }else {
            System.out.println("调用失败");
        }
        return null;
    }

    //统一收单 下单退款接口
    //退款要根据 out_trade_no(对外订单编号) 和交易金额进行退款
    @Override
    public void refund(String outTradeNo) {
        //1:查询支付表
        PaymentInfo paymentInfo = paymentService.selectPayment(outTradeNo);

        //退款接口: alipay.trade.refund
        AlipayTradeRefundRequest alipayTradeRefundRequest = new AlipayTradeRefundRequest();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("out_trade_no",paymentInfo.getOutTradeNo()); //对外订单编号
        jsonObject.put("refund_amount",paymentInfo.getTotalAmount()); //退款金额

        alipayTradeRefundRequest.setBizContent(jsonObject.toString());
        try {
            AlipayTradeRefundResponse alipayTradeRefundResponse
                    = alipayClient.pageExecute(alipayTradeRefundRequest);
            if (alipayTradeRefundResponse.isSuccess()){
                //退款成功
                System.out.println("退款成功");
            }else {
                //退款失败
                System.out.println("退款失败");
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

    }
}
